var hexcase = 0;
var b64pad = "";

function hex_sha256(b) { return rstr2hex(rstr_sha256(str2rstr_utf8(b))) }

function b64_sha256(b) { return rstr2b64(rstr_sha256(str2rstr_utf8(b))) }

function any_sha256(d, c) { return rstr2any(rstr_sha256(str2rstr_utf8(d)), c) }

function hex_hmac_sha256(d, c) { return rstr2hex(rstr_hmac_sha256(str2rstr_utf8(d), str2rstr_utf8(c))) }

function b64_hmac_sha256(d, c) { return rstr2b64(rstr_hmac_sha256(str2rstr_utf8(d), str2rstr_utf8(c))) }

function any_hmac_sha256(e, f, d) { return rstr2any(rstr_hmac_sha256(str2rstr_utf8(e), str2rstr_utf8(f)), d) }

function sha256_vm_test() { return hex_sha256("abc").toLowerCase() == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" }

function rstr_sha256(b) { return binb2rstr(binb_sha256(rstr2binb(b), b.length * 8)) }

function rstr_hmac_sha256(n, k) { var l = rstr2binb(n); if (l.length > 16) { l = binb_sha256(l, n.length * 8) } var i = Array(16),
        m = Array(16); for (var h = 0; h < 16; h++) { i[h] = l[h] ^ 909522486;
        m[h] = l[h] ^ 1549556828 } var j = binb_sha256(i.concat(rstr2binb(k)), 512 + k.length * 8); return binb2rstr(binb_sha256(m.concat(j), 512 + 256)) }

function rstr2hex(l) { try { hexcase } catch (i) { hexcase = 0 } var j = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; var e = ""; var h; for (var k = 0; k < l.length; k++) { h = l.charCodeAt(k);
        e += j.charAt((h >>> 4) & 15) + j.charAt(h & 15) } return e }

function rstr2b64(p) { try { b64pad } catch (l) { b64pad = "" } var m = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var e = ""; var i = p.length; for (var n = 0; n < i; n += 3) { var j = (p.charCodeAt(n) << 16) | (n + 1 < i ? p.charCodeAt(n + 1) << 8 : 0) | (n + 2 < i ? p.charCodeAt(n + 2) : 0); for (var o = 0; o < 4; o++) { if (n * 8 + o * 6 > p.length * 8) { e += b64pad } else { e += m.charAt((j >>> 6 * (3 - o)) & 63) } } } return e }

function rstr2any(m, t) { var u = t.length; var n = Array(); var o, v, i, r; var p = Array(Math.ceil(m.length / 2)); for (o = 0; o < p.length; o++) { p[o] = (m.charCodeAt(o * 2) << 8) | m.charCodeAt(o * 2 + 1) } while (p.length > 0) { r = Array();
        i = 0; for (o = 0; o < p.length; o++) { i = (i << 16) + p[o];
            v = Math.floor(i / u);
            i -= v * u; if (r.length > 0 || v > 0) { r[r.length] = v } }
        n[n.length] = i;
        p = r } var s = ""; for (o = n.length - 1; o >= 0; o--) { s += t.charAt(n[o]) } var q = Math.ceil(m.length * 8 / (Math.log(t.length) / Math.log(2))); for (o = s.length; o < q; o++) { s = t[0] + s } return s }

function str2rstr_utf8(j) { var f = ""; var i = -1; var g, h; while (++i < j.length) { g = j.charCodeAt(i);
        h = i + 1 < j.length ? j.charCodeAt(i + 1) : 0; if (55296 <= g && g <= 56319 && 56320 <= h && h <= 57343) { g = 65536 + ((g & 1023) << 10) + (h & 1023);
            i++ } if (g <= 127) { f += String.fromCharCode(g) } else { if (g <= 2047) { f += String.fromCharCode(192 | ((g >>> 6) & 31), 128 | (g & 63)) } else { if (g <= 65535) { f += String.fromCharCode(224 | ((g >>> 12) & 15), 128 | ((g >>> 6) & 63), 128 | (g & 63)) } else { if (g <= 2097151) { f += String.fromCharCode(240 | ((g >>> 18) & 7), 128 | ((g >>> 12) & 63), 128 | ((g >>> 6) & 63), 128 | (g & 63)) } } } } } return f }

function str2rstr_utf16le(d) { var e = ""; for (var f = 0; f < d.length; f++) { e += String.fromCharCode(d.charCodeAt(f) & 255, (d.charCodeAt(f) >>> 8) & 255) } return e }

function str2rstr_utf16be(d) { var e = ""; for (var f = 0; f < d.length; f++) { e += String.fromCharCode((d.charCodeAt(f) >>> 8) & 255, d.charCodeAt(f) & 255) } return e }

function rstr2binb(d) { var e = Array(d.length >> 2); for (var f = 0; f < e.length; f++) { e[f] = 0 } for (var f = 0; f < d.length * 8; f += 8) { e[f >> 5] |= (d.charCodeAt(f / 8) & 255) << (24 - f % 32) } return e }

function binb2rstr(d) { var e = ""; for (var f = 0; f < d.length * 32; f += 8) { e += String.fromCharCode((d[f >> 5] >>> (24 - f % 32)) & 255) } return e }

function sha256_S(c, d) { return (c >>> d) | (c << (32 - d)) }

function sha256_R(c, d) { return (c >>> d) }

function sha256_Ch(e, f, d) { return ((e & f) ^ ((~e) & d)) }

function sha256_Maj(e, f, d) { return ((e & f) ^ (e & d) ^ (f & d)) }

function sha256_Sigma0256(b) { return (sha256_S(b, 2) ^ sha256_S(b, 13) ^ sha256_S(b, 22)) }

function sha256_Sigma1256(b) { return (sha256_S(b, 6) ^ sha256_S(b, 11) ^ sha256_S(b, 25)) }

function sha256_Gamma0256(b) { return (sha256_S(b, 7) ^ sha256_S(b, 18) ^ sha256_R(b, 3)) }

function sha256_Gamma1256(b) { return (sha256_S(b, 17) ^ sha256_S(b, 19) ^ sha256_R(b, 10)) }

function sha256_Sigma0512(b) { return (sha256_S(b, 28) ^ sha256_S(b, 34) ^ sha256_S(b, 39)) }

function sha256_Sigma1512(b) { return (sha256_S(b, 14) ^ sha256_S(b, 18) ^ sha256_S(b, 41)) }

function sha256_Gamma0512(b) { return (sha256_S(b, 1) ^ sha256_S(b, 8) ^ sha256_R(b, 7)) }

function sha256_Gamma1512(b) { return (sha256_S(b, 19) ^ sha256_S(b, 61) ^ sha256_R(b, 6)) }
var sha256_K = new Array(1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998);

function binb_sha256(g, e) { var d = new Array(1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225); var h = new Array(64); var c, f, i, j, m, D, E, F; var a, b, l, C;
    g[e >> 5] |= 128 << (24 - e % 32);
    g[((e + 64 >> 9) << 4) + 15] = e; for (a = 0; a < g.length; a += 16) { c = d[0];
        f = d[1];
        i = d[2];
        j = d[3];
        m = d[4];
        D = d[5];
        E = d[6];
        F = d[7]; for (b = 0; b < 64; b++) { if (b < 16) { h[b] = g[b + a] } else { h[b] = safe_add(safe_add(safe_add(sha256_Gamma1256(h[b - 2]), h[b - 7]), sha256_Gamma0256(h[b - 15])), h[b - 16]) }
            l = safe_add(safe_add(safe_add(safe_add(F, sha256_Sigma1256(m)), sha256_Ch(m, D, E)), sha256_K[b]), h[b]);
            C = safe_add(sha256_Sigma0256(c), sha256_Maj(c, f, i));
            F = E;
            E = D;
            D = m;
            m = safe_add(j, l);
            j = i;
            i = f;
            f = c;
            c = safe_add(l, C) }
        d[0] = safe_add(c, d[0]);
        d[1] = safe_add(f, d[1]);
        d[2] = safe_add(i, d[2]);
        d[3] = safe_add(j, d[3]);
        d[4] = safe_add(m, d[4]);
        d[5] = safe_add(D, d[5]);
        d[6] = safe_add(E, d[6]);
        d[7] = safe_add(F, d[7]) } return d }

function safe_add(f, g) { var h = (f & 65535) + (g & 65535); var e = (f >> 16) + (g >> 16) + (h >> 16); return (e << 16) | (h & 65535) };

module.exports = {
    hex_sha256: hex_sha256
}

/* 使用
var SHA256 = require('../../utils/encrypt/sha256/sha256')
SHA256.hex_sha256(a)
*/